package org.oexam.ostudy.serv;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.oexam.ostudy.db.TestAccess;
import org.oexam.ostudy.facade.struct.LoggedUserDetails;
import org.oexam.ostudy.facade.struct.TestDisplayDetails;
import org.oexam.ostudy.struct.StudentAnswers;

/**
 * Servlet implementation class Test
 */
@WebServlet("/Test")
public class Test extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Test() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		LoggedUserDetails details = (LoggedUserDetails) request.getSession()
				.getAttribute("userLoginDetails");

		if (details == null) {
			response.sendRedirect("Login.jsp");
		}

		String dir = (String) request.getParameter("direction");
		if (dir == null) {
			request.getSession().setAttribute("presentQ", "-1");
			response.sendRedirect("Test?direction=next");

		}

		TestDisplayDetails testData = (TestDisplayDetails) request.getSession()
				.getAttribute("testData");

		Date presentTime = new Date();

		long left = presentTime.getTime() - testData.getStartTime().getTime();
		float seconds = (left / 1000) / 60;
		testData.setTimeLeftForDisplay(String.valueOf(testData.getMinutesLeft()
				- seconds));

		String temp = (String) request.getSession().getAttribute("presentQ");
		int i = Integer.parseInt(temp);
		if (dir != null) {
			if (dir.equals("next")) {
				i = i + 1;
				if (i >= testData.getQuestionList().size()) {
					i--;
				}

				request.getSession().setAttribute("presentQ", "" + i);
				response.sendRedirect("Test.jsp");
			} else if (dir.equals("previous")) {
				i = i - 1;
				if (i < 0) {
					i++;
				}

				request.getSession().removeAttribute("testData");
				request.getSession().setAttribute("testData", testData);
				request.getSession().setAttribute("presentQ", "" + i);
				response.sendRedirect("Test.jsp");
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@SuppressWarnings("deprecation")
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		TestDisplayDetails testData = (TestDisplayDetails) request.getSession()
				.getAttribute("testData");

		Integer presentQ = Integer.parseInt((String) request.getSession()
				.getAttribute("presentQ"));

		// TODO : Prasanna Avoid DB access from here.
		StudentAnswers answer = new StudentAnswers();
		try {
			answer.setAid(new TestAccess().getLastAnswerID() + 1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		answer.setAnswer(Integer.parseInt(request.getParameter("selAns")));
		answer.setQuestionID(testData.getQuestionList().get(presentQ).getQid());
		answer.setStudentID(testData.getStudentID());

		boolean doesAnsExist = false;
		int index = 0;
		for (StudentAnswers ans : testData.getAnswers()) {
			if (answer.getQuestionID() == ans.getQuestionID()) {
				doesAnsExist = true;
				index = testData.getAnswers().indexOf(ans);
			}

		}

		if (doesAnsExist) {
			testData.getAnswers().set(index, answer);
		} else {
			testData.getAnswers().add(answer);
		}

		request.getSession().removeAttribute("testData");
		request.getSession().setAttribute("testData", testData);
		response.sendRedirect("Test.jsp");

	}

}
